package com.zhejiangzhengyuan.municipal_way.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.SubsidiaryFacilitiesGis;
import com.zhejiangzhengyuan.municipal_common.entity.dto.FileDto;
import com.zhejiangzhengyuan.municipal_common.entity.vo.SubsidiaryFacilitiesGisVo;
import com.zhejiangzhengyuan.municipal_common.mapper.FileMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.SubsidiaryFacilitiesGisMapper;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.SubsidiaryFacilitiesGisService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

/**
 * GIS数据库中附属设施表 前端控制器
 *
 * @author linju
 * @since 2020-01-13
 */
@RestController
@Slf4j
@RequestMapping("/way/subsidiaryFacilitiesGis")
@Api(tags = "附属设施相关信息")
public class SubsidiaryFacilitiesGisController {

  @Autowired private SubsidiaryFacilitiesGisService subsidiaryFacilitiesGisService;
  @Autowired private FileMapper fileMapper;
  @Autowired private SubsidiaryFacilitiesGisMapper subsidiaryFacilitiesGisMapper;

  @ApiOperation("查询附属设施信息并分页")
  @GetMapping("/selectAllSubsidiaryFacilitiesGis")
  public ResponseVo selectAllSubsidiaryFacilitiesGis(
      @ApiParam("页码") Long page, @ApiParam("每页显示条数") Long limit) {
    page = Objects.isNull(page) || page <= 0 ? 1 : page;
    limit = Objects.isNull(limit) || limit <= 0 ? 14 : limit;
    Page<SubsidiaryFacilitiesGisVo> subsidiaryFacilitiesGisPage = new Page<>(page, limit);
    IPage<SubsidiaryFacilitiesGisVo> subsidiaryFacilitiesGisIPage =
        subsidiaryFacilitiesGisService.selectAllSubsidiaryFacilitiesGis(
            subsidiaryFacilitiesGisPage);
    return Objects.isNull(subsidiaryFacilitiesGisIPage)
        ? ResponseVo.SERVER_ERROR
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, subsidiaryFacilitiesGisIPage);
  }

  @ApiOperation("更新附属设施文件")
  @PostMapping("/updateSubsidiaryFacilitiesGis")
  public ResponseVo updateSubsidiaryFacilitiesGis(@ModelAttribute FileDto dto) {
    return subsidiaryFacilitiesGisService.updateSubsidiaryFacilitiesGis(dto) > 0
        ? ResponseVo.SUCCESS
        : ResponseVo.CLIENT_PARAM_ERROR;
  }

  @ApiOperation("附属设施模糊查询")
  @RequestMapping(value = "/fuzzySubsidiaryFacilitiesGis", method = RequestMethod.GET)
  public ResponseVo FuzzySubsidiaryFacilitiesGis(
      String name, String code, @ApiParam("页码") Long page, @ApiParam("每页显示条数") Long limit) {
    page = Objects.isNull(page) || page <= 0 ? 1 : page;
    limit = Objects.isNull(limit) || limit <= 0 ? 14 : limit;
    Page<SubsidiaryFacilitiesGisVo> subsidiaryFacilitiesGisVoPage = new Page<>(page, limit);
    Page<SubsidiaryFacilitiesGisVo> subsidiaryFacilitiesGis =
        subsidiaryFacilitiesGisMapper.FuzzySubsidiaryFacilitiesGis(
            subsidiaryFacilitiesGisVoPage, name, code);
    List<SubsidiaryFacilitiesGisVo> records = subsidiaryFacilitiesGis.getRecords();
    for (SubsidiaryFacilitiesGisVo subsidiaryFacilitiesGisVo : records) {
      List<String> strings = fileMapper.selectByBid(subsidiaryFacilitiesGisVo.getId());
      if (strings == null || strings.size() == 0) {
        subsidiaryFacilitiesGisVo.setLink(strings);
      }
    }
    return ResponseVo.successWithData(subsidiaryFacilitiesGis);
  }

  @ApiOperation("导出数据")
  @GetMapping("/export")
  public void excelRoad(HttpServletResponse response) {
    QueryWrapper<SubsidiaryFacilitiesGis> wrapper = new QueryWrapper<>();
    QueryWrapper<SubsidiaryFacilitiesGis> id = wrapper.orderByAsc("id");
    List<SubsidiaryFacilitiesGis> list = subsidiaryFacilitiesGisService.list(id);
    HashSet<String> exclude = new HashSet<>();
    exclude.add("createTime");
    exclude.add("updateTime");
    ExcelUtil.objectToExcelAndDownload(
        response, exclude, "附属设施数据表", SubsidiaryFacilitiesGis.class, list);
  }


}
